package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.invest.CreateInvestQuestionBo;
import com.zhuss.bean.bo.team.invest.DeleteInvestQuestionBo;
import com.zhuss.bean.bo.team.invest.DetailInvestBo;
import com.zhuss.bean.bo.team.invest.InvestQuestionBindBo;
import com.zhuss.bean.vo.team.invest.DetailInvestVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/30 13:42
 */
@Component("investQuestionBind")
public class InvestQuestionBind extends AbstractProcessor<Object, InvestQuestionBindBo> implements Processor<Object, InvestQuestionBindBo> {

    @Autowired
    @Qualifier("investQuestionRead")
    private InvestQuestionRead investQuestionRead;

    @Autowired
    @Qualifier("investQuestionDelete")
    private InvestQuestionDelete investQuestionDelete;

    @Autowired
    @Qualifier("investQuestionCreate")
    private InvestQuestionCreate investQuestionCreate;

    @Autowired
    @Qualifier("investRead")
    private InvestRead investRead;

    @Override
    public Object process(InvestQuestionBindBo bo) throws WpsException {
        Long investId = bo.getInvestId();
        List<Long> questionList = bo.getQuestionIdList();
        @SuppressWarnings("unchecked")
        List<Long> dbList = (List<Long>)investQuestionRead.listQuestionId(investId);
        List<Long> addList = new ArrayList<Long>(questionList);
        List<Long> removeList = new ArrayList<Long>(dbList);

        removeList.removeAll(questionList);
        addList.removeAll(dbList);

        if (!removeList.isEmpty()) {
            DeleteInvestQuestionBo param = new DeleteInvestQuestionBo();
            param.setInvestId(investId);
            param.setQuestionIdList(removeList);

            param.setModifierId(bo.getConcurrentUserId());
            param.setModifier(bo.getConcurrentUserName());
            param.setModifyTime(new Date());
            investQuestionDelete.process(param);
        }

        for (Long questionId : addList) {
            CreateInvestQuestionBo param = new CreateInvestQuestionBo();
            BeanUtils.copyProperties(bo, param);
            param.setQuestionId(questionId);
            param.setInvestId(investId);
            investQuestionCreate.process(param);
        }

        String name = ((DetailInvestVo) investRead.detail(investId)).getInvestName();
        bo.setName(name);
        bo.setAddList(addList);
        bo.setRemoveList(removeList);
        return null;
    }

    @Override
    public Object log(InvestQuestionBindBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.协同发布.key());
        record.setAction(LogActionEnum.问卷问题绑定.key());
        record.setTargetId(param.getInvestId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(InvestQuestionBindBo investQuestionBindBo) throws WpsException {

    }
}
